package com.syezon.wifikey.news.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.syezon.wifikey.ad.ConfigService;
import com.syezon.wifikey.news.config.NewsConfig;
import com.syezon.wifikey.news.data.NewsData;
import com.syezon.wifikey.news.info.NewsInfo;
import com.syezon.wifikey.news.info.NewsItemInfo;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/9/30.
 */

public class NewsViewModel extends AndroidViewModel {

    private static final List<NewsItemInfo> EMPTY_LIST = new ArrayList<>();

    private MutableLiveData<List<NewsItemInfo>> refreshMutableLiveData = new MutableLiveData<>();
    private MutableLiveData<List<NewsItemInfo>> loadMoreMutableLiveData = new MutableLiveData<>();

    private int pageNum = 2;

    public NewsViewModel(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<List<NewsItemInfo>> getRefreshMutableLiveData() {
        return refreshMutableLiveData;
    }

    public MutableLiveData<List<NewsItemInfo>> getLoadMoreMutableLiveData() {
        return loadMoreMutableLiveData;
    }

    public void refreshNews(final boolean isFirstTime) {
        Observable.create(new ObservableOnSubscribe<List<NewsItemInfo>>() {
            @Override
            public void subscribe(ObservableEmitter<List<NewsItemInfo>> e) throws Exception {
                List<NewsItemInfo> newsInfos = new ArrayList<>();
                List<NewsInfo> newsRecommendList = NewsData.getNewsRecommendList(isFirstTime, ConfigService.getInstance().isShowSelfAd(), NewsConfig.NEWS_SOURCE, 1);
                pageNum = 2;
                for (int i = 0; newsRecommendList != null && i < newsRecommendList.size(); i++) {
                    NewsInfo newsInfo = newsRecommendList.get(i);
                    NewsItemInfo newsItemInfo = toNewsItemInfo(newsInfo);
                    newsInfos.add(newsItemInfo);
                }
                e.onNext(newsInfos);
            }
        }).subscribeOn(Schedulers.io())
                .subscribe(new Observer<List<NewsItemInfo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<NewsItemInfo> newsInfos) {
                        getRefreshMutableLiveData().postValue(newsInfos);
                    }

                    @Override
                    public void onError(Throwable e) {
                        getRefreshMutableLiveData().postValue(EMPTY_LIST);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void loadMoreNews() {
        Observable.create(new ObservableOnSubscribe<List<NewsItemInfo>>() {
            @Override
            public void subscribe(ObservableEmitter<List<NewsItemInfo>> e) throws Exception {
                List<NewsItemInfo> newsInfos = new ArrayList<>();
                List<NewsInfo> newsRecommendList = NewsData.getNewsRecommendList(false, ConfigService.getInstance().isShowSelfAd(), NewsConfig.NEWS_SOURCE, pageNum);
                for (int i = 0; newsRecommendList != null && i < newsRecommendList.size(); i++) {
                    NewsInfo newsInfo = newsRecommendList.get(i);
                    NewsItemInfo newsItemInfo = toNewsItemInfo(newsInfo);
                    newsInfos.add(newsItemInfo);
                }
                if (newsInfos.size() > 0) {
                    pageNum++;
                }
                e.onNext(newsInfos);
            }
        }).subscribeOn(Schedulers.io())
                .subscribe(new Observer<List<NewsItemInfo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<NewsItemInfo> newsInfos) {
                        getLoadMoreMutableLiveData().postValue(newsInfos);
                    }

                    @Override
                    public void onError(Throwable e) {
                        getLoadMoreMutableLiveData().postValue(EMPTY_LIST);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private NewsItemInfo toNewsItemInfo(NewsInfo newsInfo) {
        NewsItemInfo newsItemInfo = new NewsItemInfo();
        if (newsInfo.infoType == NewsInfo.NEWS_TYPE_SINGLE_PIC) {
            newsItemInfo.setType(NewsItemInfo.TYPE_NEWS_SINGLE_PIC);
        } else if (newsInfo.infoType == NewsInfo.NEWS_TYPE_SIMPLE_TEXT) {
            newsItemInfo.setType(NewsItemInfo.TYPE_NEWS_SIMPLE_TEXT);
        } else if (newsInfo.infoType == NewsInfo.NEWS_TYPE_THREE_PIC) {
            newsItemInfo.setType(NewsItemInfo.TYPE_NEWS_PICS);
        } else if (newsInfo.infoType == NewsInfo.NEWS_TYPE_BIG_PIC) {
            newsItemInfo.setType(NewsItemInfo.TYPE_NEWS_BIG_PIC);
        } else {
            newsItemInfo.setType(NewsItemInfo.TYPE_NEWS_SIMPLE_TEXT);
        }
        newsItemInfo.setNewsInfo(newsInfo);
        return newsItemInfo;
    }

}
